package io.shdxhl.simplerpc.code.netty.strategy.impl;

import io.shdxhl.simplerpc.client.provider.ServiceProvider;
import io.shdxhl.simplerpc.code.annotation.SimpleLoadBalance;
import io.shdxhl.simplerpc.code.netty.strategy.LoadBalanceStrategy;
import org.apache.commons.lang3.RandomUtils;

import java.util.List;

/**
 * 随机数负载均衡
 */
@SimpleLoadBalance(strategy = "random")
public class RandomLoadBalanceStrategy implements LoadBalanceStrategy {
    @Override
    public ServiceProvider select(List<ServiceProvider> serviceProviders) {
        int index = RandomUtils.nextInt(0, serviceProviders.size());
        return serviceProviders.get(index);
    }
}
